//
//  ViewController.m
//  time
//
//  Created by  Mac on 2017/8/21.
//  Copyright © 2017年  Mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)点击获取时间戳:(UIButton *)sender {
    
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSTimeInterval a=[dat timeIntervalSince1970];
    
    NSString * timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    
//    NSLog(@"当前时间戳 字符串格式 == %@",timeString);
    
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    
    
    NSString * hAndmAnds = [dateString substringFromIndex:11];
//    NSLog(@"时分秒 = %@",hAndmAnds);
    
    NSString * h = [hAndmAnds substringToIndex:2];
//    NSLog(@"时 = %@",h);
    
    NSString * mAnds = [hAndmAnds substringFromIndex:3];
//    NSLog(@"分和秒 = %@",mAnds);
    
    NSString * m = [mAnds substringToIndex:2];
    
//    NSLog(@"分 = %@",m);
    
    
    NSString * s = [mAnds substringFromIndex:3];
    
//    NSLog(@"秒 = %@",s);
    
    
    double dh = [h doubleValue] * 3600;
    double dm = [m doubleValue] * 60;
    double ds = [s doubleValue];
    
    //    今天零点的时间戳
    double  startTime = [timeString doubleValue] - dh - dm - ds;
    
    //当前时间戳
    
    double nowTime = [timeString doubleValue];
    
    //    今天24点的时间戳
    double  endTime = [timeString doubleValue] - dh - dm - ds + 86400.0;
    
    if(endTime > startTime)
    {
        //    测试时间戳大小比较（浮点型）
    }
    
    if(nowTime > endTime)
    {
        //    测试时间戳大小比较（浮点型）
    }
    
    NSLog(@"\n今天0点的时间戳：%f \n当前时间戳: %f \n今天24点的时间戳：%f",startTime,nowTime,endTime);
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
